﻿Imports System.Data.SqlClient

Public Class clsInterfazNominas
    Private mintIdMovtoPDO As Integer
    Private mintIdPeriodo As Integer
    Private mintIdEmpleado As Integer
    Private mintIdConcepto As Integer
    Private mintIdMovtoPermanente As Integer
    Private mdblImporteTotal As Double
    Private mdblValor As Double
    Private mdblImporte1 As Double
    Private mdblImporte2 As Double
    Private mdblImporte3 As Double
    Private mdblImporte4 As Double
    Private mbolImporteTotalReportado As Integer
    Private mbolImporte1Reportado As Integer
    Private mbolImporte2Reportado As Integer
    Private mbolImporte3Reportado As Integer
    Private mbolImporte4Reportado As Integer
    Private mdatTimeStamp As Date
    Public Property IdMovtoPDO() As Integer
        Get
            Return mintIdMovtoPDO
        End Get
        Set(ByVal value As Integer)
            mintIdMovtoPDO = value
        End Set
    End Property
    Public Property IdPeriodo() As Integer
        Get
            Return mintIdPeriodo
        End Get
        Set(ByVal value As Integer)
            mintIdPeriodo = value
        End Set
    End Property
    Public Property IdEmpleado() As Integer
        Get
            Return mintIdEmpleado
        End Get
        Set(ByVal value As Integer)
            mintIdEmpleado = value
        End Set
    End Property
    Public Property IdConcepto() As Integer
        Get
            Return mintIdConcepto
        End Get
        Set(ByVal value As Integer)
            mintIdConcepto = value
        End Set
    End Property
    Public Property IdMovtoPermanente() As Integer
        Get
            Return mintIdMovtoPermanente
        End Get
        Set(ByVal value As Integer)
            mintIdMovtoPermanente = value
        End Set
    End Property
    Public Property ImporteTotal() As Double
        Get
            Return mdblImporteTotal

        End Get
        Set(value As Double)
            mdblImporteTotal = value
        End Set
    End Property
    Public Property Valor() As Double
        Get
            Return mdblValor

        End Get
        Set(value As Double)
            mdblValor = value
        End Set
    End Property
    Public Property Importe1() As Double
        Get
            Return mdblImporte1

        End Get
        Set(value As Double)
            mdblImporte2 = value
        End Set
    End Property
    Public Property Importe2() As Double
        Get
            Return mdblImporte2

        End Get
        Set(value As Double)
            mdblImporte2 = value
        End Set
    End Property
    Public Property Importe3() As Double
        Get
            Return mdblImporte3

        End Get
        Set(value As Double)
            mdblImporte3 = value
        End Set
    End Property
    Public Property Importe4() As Double
        Get
            Return mdblImporte4

        End Get
        Set(value As Double)
            mdblImporte4 = value
        End Set
    End Property
    Public Property ImporteTotalReportado() As Integer
        Get
            Return mbolImporteTotalReportado
        End Get
        Set(value As Integer)
            mbolImporteTotalReportado = value
        End Set
    End Property
    Public Property Importe1Reportado() As Integer
        Get
            Return mbolImporte1Reportado
        End Get
        Set(value As Integer)
            mbolImporte1Reportado = value
        End Set
    End Property
    Public Property Importe2Reportado() As Integer
        Get
            Return mbolImporte2Reportado
        End Get
        Set(value As Integer)
            mbolImporte2Reportado = value
        End Set
    End Property
    Public Property Importe3Reportado() As Integer
        Get
            Return mbolImporte3Reportado
        End Get
        Set(value As Integer)
            mbolImporte3Reportado = value
        End Set
    End Property
    Public Property Importe4Reportado() As Integer
        Get
            Return mbolImporte4Reportado
        End Get
        Set(value As Integer)
            mbolImporte4Reportado = value
        End Set
    End Property
    Public Property TimeStamp As Date
        Get
            Return mdatTimeStamp
        End Get
        Set(value As Date)
            mdatTimeStamp = value
        End Set
    End Property

    Public Sub New()
        mintIdMovtoPDO = 0
        mintIdPeriodo = 0
        mintIdEmpleado = 0
        mintIdConcepto = 0
        mintIdMovtoPermanente = 0
        mdblImporteTotal = 0
        mdblValor = 0
        mdblImporte1 = 0
        mdblImporte2 = 0
        mdblImporte3 = 0
        mdblImporte4 = 0
        mbolImporteTotalReportado = 0
        mbolImporte1Reportado = 0
        mbolImporte2Reportado = 0
        mbolImporte3Reportado = 0
        mbolImporte4Reportado = 0
        mdatTimeStamp = Now()
    End Sub
    Public Sub Insertar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = NominasConnection

        Dim f As String = mdatTimeStamp.ToString("MM/dd/yyyy")
        'Dim f As String = mdatTimeStamp.ToString("s")

        strSqlQry = "Insert Into NOM10008 (IdPeriodo,IdEmpleado,IdConcepto,IdMovtoPermanente,ImporteTotal,Valor,Importe1,Importe2,Importe3,Importe4,ImporteTotalReportado,Importe1Reportado,Importe2Reportado,Importe3Reportado,Importe4Reportado,TimeStamp) " & _
                    "                  Values (" & _
                    " " & mintIdPeriodo & "," & _
                    " " & mintIdEmpleado & "," & _
                    " " & mintIdConcepto & "," & _
                    " " & mintIdMovtoPermanente & "," & _
                    " " & mdblImporteTotal & "," & _
                    " " & mdblValor & "," & _
                    " " & mdblImporte1 & "," & _
                    " " & mdblImporte2 & "," & _
                    " " & mdblImporte3 & "," & _
                    " " & mdblImporte4 & "," & _
                    " " & mbolImporteTotalReportado & "," & _
                    " " & mbolImporte1Reportado & "," & _
                    " " & mbolImporte2Reportado & "," & _
                    " " & mbolImporte3Reportado & "," & _
                    " " & mbolImporte4Reportado & "," & _
                    " " & f & ")"


        Dim cmdInsert As New SqlCommand
        cmdInsert.Connection = cnConn
        cmdInsert.CommandText = strSqlQry

        Try
            cnConn.Open()
            cmdInsert.ExecuteNonQuery()
            cnConn.Close()


        Catch ex As SqlException
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally

            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If

        End Try
    End Sub
    Public Sub Actualizar()
        Dim strSqlQry As String

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = NominasConnection

        Dim f As String = mdatTimeStamp.ToString("MM/dd/yyyy")
        'Dim f As String = mdatTimeStamp.ToString("s")

        strSqlQry = "Update NOM10008 Set " & _
                    " ImporteTotal=" & mdblImporteTotal & "," & _
                    " TimeStamp=" & f & " " & _
                    " where IdMovtoPDO=" & mintIdMovtoPDO

        Dim cmdInsert As New SqlCommand
        cmdInsert.Connection = cnConn
        cmdInsert.CommandText = strSqlQry


        Try

            cnConn.Open()
            cmdInsert.ExecuteNonQuery()
            cnConn.Close()

        Catch ex As SqlException
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally

            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If

        End Try

    End Sub


    Function consecutivo() As Double
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = NominasConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT Max(IdMovtoPDO) As id From NOM10008 "
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            consecutivo = rdBuscar("ID") + 1
        Else
            consecutivo = 1
        End If

        rdBuscar.Close()
        cnConn.Close()
    End Function
End Class
